let db = require('../db/index')
// 获取role数据
const getRole = (req, res) => {
    let sql = 'select * from role order by roleId'
    db.query(sql, (err, result) => {
        if (err) {
            res.send({
                status: 400,
                message: '查询失败'
            })
            return
        } else {
            res.send({
                status: 200,
                message: '查询成功',
                result: result
            })
        }
    })
}
// 添加role数据
const addRole = (req, res) => {
    let sql = `insert into role (roleName) values(?)`
    db.query(sql, [req.body.roleName], (err, result) => {
        if (err) {
            res.send({
                status: 400,
                message: '查询失败'
            })
            return
        } else {
            res.send({
                status: 200,
                message: '查询成功',
                result: result
            })
        }
    })
}
// 更新修改role数据
const updateRole = (req, res) => {
    let sql = `update role set roleName=? where roleId=?`
    db.query(sql, [req.body.roleName, req.body.roleId], (err, result) => {
        if (err) {
            res.send({
                status: 400,
                message: '修改失败'
            })
            return
        } else {
            res.send({
                status: 200,
                message: "修改成功"
            })
        }
    })
}
// 删除role数据
const deleteRole = (req, res) => {
    let sql = `delete from role where roleId=?`
    db.query(sql, [req.body.roleId], (err, result) => {
        if (err) {
            res.send({
                status: 400,
                message: '查询失败'
            })
            return
        } else {
            res.send({
                status: 200,
                message: '查询成功',
                result: result
            })
        }
    })
}
// 获取角色名称
const getRoleName = (req, res) => {
    let sql = 'select roleName from role'
    db.query(sql, (err, result) => {
        if (err) {
            res.send({
                status: 400,
                message: '查询失败'
            })
            return
        } else {
            res.send({
                status: 200,
                message: '查询成功',
                result: result
            })
        }
    })
}
module.exports = {
    getRole, addRole, updateRole, deleteRole, getRoleName
}